//
//  SearchViewController.m
//  MMLOOK
//
//  Created by dllo on 15/9/3.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "SearchViewController.h"
#import "FoundViewController.h"
#import "SouSouCollectionViewCell.h"
#import "JudgeConnection.h"
#import "Alert.h"

@interface SearchViewController ()<UISearchBarDelegate,UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UISearchBar *searchBar;
@property (nonatomic, retain)NSMutableArray *collectArray;
@property (nonatomic, retain)NSString *searchText;
@property (nonatomic, retain)NSString *text;
@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)NSMutableArray *array;

@end

@implementation SearchViewController

- (void)dealloc
{
    [_searchBar release];
    [_collectArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.collectArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.searchBar.text = @"";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatCollection];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:253/255.0 green:180/255.0 blue:83/255.0 alpha:1];
    self.searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 45)] autorelease];
    _searchBar.backgroundColor = [UIColor colorWithRed:253/255.0 green:180/255.0 blue:83/255.0 alpha:1];
    _searchBar.placeholder = @"例如衣服、蛋糕 . . .";
    _searchBar.barStyle = UIBarStyleDefault;
    _searchBar.delegate = self;
    self.navigationItem.titleView = _searchBar;
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick:)] autorelease];
}

- (void)creatCollection
{
    UILabel *titleLable = [[[UILabel alloc] initWithFrame:CGRectMake(20 * OffWidth, 70 * OffHeight, 70 * OffWidth, 30 * OffHeight)] autorelease];
    titleLable.text = @"搜搜看";
    titleLable.backgroundColor = [UIColor whiteColor];
    titleLable.font = [UIFont systemFontOfSize:20 * OffHeight];
    titleLable.textColor = [UIColor orangeColor];
    [self.view addSubview:titleLable];
    
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.itemSize = CGSizeMake(self.view.frame.size.width / 3, 50);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    self.collection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 150) collectionViewLayout:layout] autorelease];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [_collection registerClass:[SouSouCollectionViewCell class] forCellWithReuseIdentifier:@"look"];
    [self.view addSubview:_collection];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 9;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SouSouCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"look" forIndexPath:indexPath];
    
    self.array = [NSMutableArray arrayWithObjects:@"樱花", @"手工", @"泳衣", @"手表", @"双肩包", @"戒指", @"手链",@"手机壳", @"项链", nil];
    
    cell.lable.text = [_array objectAtIndex:indexPath.row];
    cell.lable.textColor = [UIColor lightGrayColor];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([JudgeConnection judgeConnection]) {
        
        self.searchBar.text = [_array objectAtIndex:indexPath.item];
        self.searchText = self.searchBar.text;
        [_searchBar resignFirstResponder];
        [self searchVC];
    } else {
        
        Alert *alert = [[[Alert alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 250) / 2, self.view.frame.size.height * 3 / 4, 250, 50) LabelText:@"当前无网络连接，请检查网络状态"] autorelease];
        [self.view addSubview:alert];
    }
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    searchBar.text = searchText;
    self.searchText = [searchText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [_searchBar resignFirstResponder];
    [self searchVC];
}

- (void)searchVC
{
    FoundViewController *found = [[[FoundViewController alloc] init] autorelease];
    found.searchtext = self.searchText;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:self.searchBar.text style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationController pushViewController:found animated:YES];
}

- (void)buttonClick: (UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    self.searchBar.text  = @"";
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
